#include "fileview.h"
#include "ui_fileview.h"
#include "ui/iconloader.h"
#include "core/mimedata.h"

#include <QDebug>
#include <QScrollBar>

FileView::FileView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FileView),
    model_(nullptr),
    undo_stack_(new QUndoStack(this))
{
    ui->setupUi(this);
    ui->back->setIcon(IconLoader::Load("go-previous", IconLoader::Base));
    ui->forward->setIcon(IconLoader::Load("go-next", IconLoader::Base));
    ui->home->setIcon(IconLoader::Load("go-home", IconLoader::Base));
    ui->up->setIcon(IconLoader::Load("go-up", IconLoader::Base));

    //connect(ui->back, SIGNAL(clicked()), undo_stack_, SLOT(undo()));
    //connect(ui->forward, SIGNAL(clicked()), undo_stack_, SLOT(redo()));
    connect(ui->home, SIGNAL(clicked()), SLOT(FileHome()));
    connect(ui->up, SIGNAL(clicked()), SLOT(FileUp()));
    connect(ui->path, SIGNAL(textChanged(QString)),
            SLOT(ChangeFilePath(QString)));
    connect(ui->list, SIGNAL(activated(QModelIndex)),
            SLOT(ItemActivated(QModelIndex)));

    connect(ui->list, SIGNAL(doubleClicked(QModelIndex)),
            SLOT(ItemDoubleClick(QModelIndex)));
}

void FileView::ChangeFilePathWithoutUndo(const QString& new_path) {
    ui->list->setRootIndex(model_->setRootPath(new_path));
    ui->path->setText(QDir::toNativeSeparators(new_path));

    QDir dir(new_path);
    ui->up->setEnabled(dir.cdUp());

    emit PathChanged(new_path);
}

void FileView::showEvent(QShowEvent * event)
{

    if(model_)
        return;
    model_ = new QFileSystemModel(this);
    ui->list->setModel(model_);
    ChangeFilePathWithoutUndo(QDir::homePath());
}

void FileView::ChangeFilePath(const QString& new_path_native) {
    qInfo()<<"ChangeFilePath called";
    QString new_path = QDir::fromNativeSeparators(new_path_native);

    QFileInfo info(new_path);
    if (!info.exists() || !info.isDir())
        return;

    QString old_path(model_->rootPath());
    if (old_path == new_path)
        return;

    undo_stack_->push(new UndoCommand(this, new_path));
}


void FileView::ItemActivated(const QModelIndex& index) {
    if (model_->isDir(index))
        ChangeFilePath(model_->filePath(index));
}

void FileView::ItemDoubleClick(const QModelIndex& index) {

    qInfo()<<"ItemDoubleClick called";
    if (model_->isDir(index))
        return;


    QString file_path = model_->filePath(index);

    MimeData* data = new MimeData;
    data->from_doubleclick_ = true;
    data->setUrls(QList<QUrl>() << QUrl::fromLocalFile(file_path));
    data->name_for_new_playlist_ = file_path;

    emit AddToPlaylist(data);
}

FileView::UndoCommand::UndoCommand(FileView* view, const QString& new_path)
    : view_(view) {
    old_state_.path = view->model_->rootPath();
    old_state_.scroll_pos = view_->ui->list->verticalScrollBar()->value();
    old_state_.index = view_->ui->list->currentIndex();

    new_state_.path = new_path;
}

void FileView::UndoCommand::redo() {

    qInfo()<<"UndoCommand called";
    view_->ChangeFilePathWithoutUndo(new_state_.path);
    if (new_state_.scroll_pos != -1) {
        view_->ui->list->setCurrentIndex(new_state_.index);
        view_->ui->list->verticalScrollBar()->setValue(new_state_.scroll_pos);
    }
}

void FileView::UndoCommand::undo() {
    new_state_.scroll_pos = view_->ui->list->verticalScrollBar()->value();
    new_state_.index = view_->ui->list->currentIndex();

    view_->ChangeFilePathWithoutUndo(old_state_.path);
    view_->ui->list->setCurrentIndex(old_state_.index);
    view_->ui->list->verticalScrollBar()->setValue(old_state_.scroll_pos);
}



void FileView::keyPressEvent(QKeyEvent* e) {
    switch (e->key()) {
    case Qt::Key_Back:
    case Qt::Key_Backspace:
        ui->up->click();
        break;
    case Qt::Key_Enter:
    case Qt::Key_Return:
        ItemDoubleClick(ui->list->currentIndex());
        break;
    }
    QWidget::keyPressEvent(e);
}


FileView::~FileView()
{
    delete ui;
}
